package servlet;


import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/SearchAllServlet.do")
public class SearchAllServlet extends HttpServlet {
    // SearchUserServlet.do
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            List<Map<String, Object>> searchResults = allquiry();

            HttpSession session = request.getSession();
            session.setAttribute("searchResults", searchResults);

            response.sendRedirect(request.getContextPath() + "/admin/usermanager.jsp");
        }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    private List<Map<String, Object>> performSearch(String searchUsername) {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE username = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, searchUsername);

            while (resultSet.next()) {
                int id = resultSet.getInt("user_id");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");
                int state = resultSet.getInt("state");
                int role = resultSet.getInt("role");

                // 检查是否存在 null 值，避免空指针异常
                if (gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "id", id,
                            "username", searchUsername,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description,
                            "state", state,
                            "role", role
                    );


                    // 将用户信息Map添加到列表中
                    userList.add(user);
                }
            }
            // 处理数据库异常
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

        return userList;
    }
    private List<Map<String, Object>> performSearchid(String id) {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE user_id = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, id);

            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");
                int state = resultSet.getInt("state");
                int role = resultSet.getInt("role");

                // 检查是否存在 null 值，避免空指针异常
                if (gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "id", id,
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description,
                            "state", state,
                            "role", role
                    );


                    // 将用户信息Map添加到列表中
                    userList.add(user);
                }
            }
            // 处理数据库异常
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

        return userList;
    }

    private List<Map<String, Object>> allquiry() {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            String sql = "SELECT * FROM 02_38_yangyang"; // 假设数据库表名为 users
            ResultSet resultSet = jdbcUtil.query(sql);
            while (resultSet.next()) {
                int id = resultSet.getInt("user_id");
                String username=resultSet.getString("username");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");
                int state = resultSet.getInt("state");
                int role = resultSet.getInt("role");

                // 检查是否存在 null 值，避免空指针异常
                if (gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "id", id,
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description,
                            "state", state,
                            "role", role
                    );


                    userList.add(user);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 处理数据库异常
            // 可以跳转到错误页面或返回错误信息
        } finally {
            jdbcUtil.closeConnection();
        }
        return  userList;
    }
}


